//
//  WHM_WSBS_ItemList_ViewController.m
//  EpointFrame5
//
//  Created by AndLi on 15/4/22.
//  Copyright (c) 2015年 Epoint. All rights reserved.
//  网上办事 事项列表

#import "WHM_WSBS_ItemList_ViewController.h"
#import "EpointWebInfoListViewController.h"
#import "WSBSItemListModel.h"
#import "WSBSItemCell.h"
#import "MJRefresh.h"

#import "WMH_WSBS_ItemDetail_ViewController.h"

@interface WHM_WSBS_ItemList_ViewController ()
{
    NSMutableArray *dataArray;
    UITableView *tv;
    UISearchBar *sb;
    UISearchDisplayController *searchController;
    MJRefreshFooterView *_footer;
    MJRefreshHeaderView *_header;
    //列表索引
    int pageIndex;
    
    //每页列表数量
    int listPageCount;
    
    //搜索关键字
    NSString *keyWork;
    
    //字体大小
    double list_title_fontsize;
    double list_date_fontsize;
    //标题字数
    int list_title_length;
    
    NSString *categuid;
    
}

@end

@implementation WHM_WSBS_ItemList_ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    categuid = [self.passParam objectForKey:@"categuid"];
    
    pageIndex = 0;
    listPageCount = 20;
    keyWork = @"";
    
    list_title_fontsize = [[DBFrameService getConfigValue:WMH_InfoList_Title_FontSize]  floatValue];
    list_date_fontsize  = [[DBFrameService getConfigValue:WMH_InfoList_Date_FontSize]   floatValue];
    list_title_length   = [[DBFrameService getConfigValue:WMH_InfoList_Title_Length]    floatValue];
    
    dataArray = [[NSMutableArray alloc]init];
    
    tv = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, AppWidth, AppHeight-SystemTakeHeight)];
    tv.rowHeight = 75;
    tv.dataSource = self;
    tv.delegate = self;
    
    [self.view addSubview:tv];
    
    [self getData];
    
//    [self addHeader];
//    [self addFooter];
}

- (BOOL)swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:(SWTableViewCell *)cell {
    return YES;
}


- (void)addFooter
{
    MJRefreshFooterView *footer = [MJRefreshFooterView footer];
    footer.scrollView = tv;
    footer.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        pageIndex++;
//        [self getData:pageIndex taskid:1];
    };
    _footer = footer;
}

-(void)addHeader
{
    MJRefreshHeaderView *header = [MJRefreshHeaderView header];
    header.scrollView = tv;
    header.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
//        [self getData:1 taskid:2];
    };
    _header = header;
}

-(void)getData
{
    [self showMBPHUD];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:categuid          forKey:@"CateGuid"];
    [self startTask:@"Task_GetWsbsItem" withParams:params withTaskId:1];
}

-(void)refreshUIWithThreadGuid:(int)taskId andData:(NSObject *)obj
{
    [self hideMBPHUD];
    //处理异常信息
    NSMutableArray *arr = (NSMutableArray*)obj;
    if (![[arr objectAtIndex:0]isEqualToString:@"1"]) {
        [self toastMessage:[arr objectAtIndex:1]];
        [_footer endRefreshing];
        [_header endRefreshing];
        return;
    }
    
    if (taskId==1) {
        
        NSMutableArray *listData = [arr objectAtIndex:2];
        
        for (WSBSItemListModel *m in listData) {
            [dataArray addObject:m];
        }
        [tv reloadData];
        [searchController.searchResultsTableView reloadData];
        [_footer endRefreshing];
        if (listData.count<listPageCount&&keyWork.length==0) {
            [_footer removeFromSuperview];
        }
    }
    else if (taskId==2)
    {
        NSMutableArray *listData = [arr objectAtIndex:2];
        NSMutableArray *enableData = [[NSMutableArray alloc]init];
        for (WSBSItemListModel *m in listData) {
            WSBSItemListModel *om = [dataArray objectAtIndex:0];
            NSString *firstInfoGuid = om.CateGuid;
            if ([m.CateGuid isEqualToString:firstInfoGuid]) {
                break;
            }
            else
            {
                [enableData addObject:m];
            }
        }
        for (int i=enableData.count-1; i>=0; i--) {
            WSBSItemListModel *sm = [enableData objectAtIndex:i];
            [dataArray insertObject:sm atIndex:0];
        }
        [_header endRefreshing];
        [tv reloadData];
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *WebinfolistItem = @"WSBSItemCell";
    WSBSItemCell *cell = (WSBSItemCell *)[tableView dequeueReusableCellWithIdentifier:WebinfolistItem];
    
    if(cell==nil){
        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"WSBSItemCell"
                                                    owner:self
                                                  options:nil];
        cell = [nib objectAtIndex:0];
        
    }
    NSUInteger row = [indexPath row];
    WSBSItemListModel *info = [dataArray objectAtIndex:row];
    cell.titleLabel.text = info.CateName;
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    WSBSItemListModel *m = [dataArray objectAtIndex:indexPath.row];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:m.CateGuid  forKey:@"CateGuid"];
    [params setObject:m.CateName  forKey:@"CateName"];

    WMH_WSBS_ItemDetail_ViewController *view = [[WMH_WSBS_ItemDetail_ViewController alloc]init];
    [params setObject:self.title forKey:@"title"];
    view.passParam = params;
    [self.navigationController pushViewController:view animated:YES];
}

@end

